const {Card, Button, Input, Select, Icon, TreeSelect, DatePicker,Row, Col} = antd;
const {MonthPicker, RangePicker} = DatePicker;
const InputGroup = Input.Group;
const Option = Select.Option;
const TreeNode = TreeSelect.TreeNode;


class SelectProject extends React.Component {
    state = mountData;
    componentDidMount() {
        var _this = this;
        if(vui.GetCookie("ProjectId") == null) {
            
            JobProject.map(function(target,index) {
                if(target.type == "1") {
                    _this.setState({value : target.text});
                    vui.Cookie(
                        { name: "ProjectId", value: target.id }
                    )
                    return 
                }
                console.log(target);
            })
        } else {
            JobProject.map(function(target,index){
                if(target.id == vui.GetCookie("ProjectId")) {
                    _this.setState({value : target.text});
                }
            })
        }
    }
    render() {
        var Data = JobProject;
        var newData = [];
        function insertData (data,arr,hasDisabled) {
            data.map(function (target,index) {
                var flag = true;
                Data.map(function (_target,_index) {
                    if (_target.id == target.parent) {
                        flag = false;
                    }
                })
                if (flag) {
                    newData.push({
                        label: target.text,
                        value: target.text,
                        key: target.id,
                        disabled:hasDisabled==true? target.type == "0"? true:false : false,
                        children:[]
                    })
                } else {
                    if (target.parent == "" || target.parent == "#" ||  target.parent == undefined) {
                        newData.push(
                            {
                                label: target.text,
                                value: target.text,
                                key: target.id,
                                disabled:hasDisabled==true? target.type == "0"? true:false :false,
                                children:[]
                            }
                        )
                    } else {
                        function ChildrenMap(array) {
                            array.map(function (_target,_index) {
                                if (target.parent == _target.key) {
                                    _target.children.push({
                                        label: target.text,
                                        value: target.text,
                                        key: target.id,
                                        disabled:hasDisabled==true? target.type == "0"? true:false :false,
                                        children:[]
                                    })
                                }
                                ChildrenMap(_target.children)
                            })
                        }
                        ChildrenMap(arr)
                    }
                }
            })
        }
        insertData(Data,newData,true);
        return (
            <InputGroup compact>
                <Input addonBefore={SelectProjectMsg.addonBefore} style={{ width: '80px' }} defaultValue="" />
                <TreeSelect
                    treeData={newData}
                    
                    showSearch
                    style={{width: "calc(100% - 80px)",marginLeft:"-1px"}}
                    value={this.props.value}
                                                    
                    dropdownStyle={{maxHeight: 400, overflow: 'auto'}}
                    placeholder={SelectProjectMsg.placeholder}
                    allowClear
                    treeDefaultExpandAll
                    onChange={this.props.selectOnChange}
                 />
                   
            </InputGroup>
        );
    }
}